import {messages} from "share/common";
import React, {Component} from 'react';
import { connect } from "react-redux";
import {Form, Input, Switch} from "antd";
import Info from "components/info";
import InputLanguage from "components/template/input-language";
import Priority from "components/template/priority/priority";
import taxCodeService from "../../tax-code.service"

const FormItem = Form.Item;

@Form.create()
@connect(mapStateToProps)
class BlackWhiteBasicInfo extends Component {

  state = {
    listCode: ''
  };

  infoData = {
    attributes: [{
      title: '',
      dataIndex: 'explain',
      span: 24,
      widthFix: false,
      colon: false
    }],
    dataSource: {
      explain: messages('expense-14.key527')/*请先维护基本信息*/
    }
  };

  componentDidMount() {
    const { record } = this.props;
    if (!record.expReceiptMappingListId) {
      this.getCode();
    }
  }

  getCode = () => {
    taxCodeService.getWhiteBlackCode().then(res => {
      this.setState({ listCode: res.data })
    })
  };

  render() {
    const { getFieldDecorator } = this.props.form;
    const { tenant, record, disabled } = this.props;
    const { listCode } = this.state;
    return (
      <div>
        <Info
          paragraph
          dataSource={this.infoData.dataSource}
          attributes={this.infoData.attributes}
        />
        <Form className='new-form'>
          <FormItem label={messages('common.column.status')/*状态*/}>
            {getFieldDecorator('enabled', {
              valuePropName: 'checked',
              initialValue: record.expReceiptMappingListId ? record.enabled : true,
            })(
              <Switch disabled={record.readOnly}/>
            )}
          </FormItem>
          <Priority
            top="TENANT"
            disabled
            disabledTop
            showCompany={false}
            showCompanyComponent={false}
            defaultValue={{
              levelCode: 'TENANT',
              levelOrgId: tenant.id,
              levelOrgName: tenant.tenantName,
            }}
            formItemLayout={{ labelCol: { span: 8 }, wrapperCol: { span: 10 } }}
          />
          <FormItem label={messages('expense-14.key528')/*编码*/}>
            {getFieldDecorator('listCode', {
              rules: [{
                required: true,
                message: messages('common.please.input')/*请输入*/
              }],
              initialValue: record.listCode || listCode
            })(
              <Input disabled={!!record.expReceiptMappingListId} maxLength={8}/>
            )}
          </FormItem>
          <FormItem label={messages('expense-14.key529')/*名称*/}>
            {getFieldDecorator('listNameI18n', {
              rules: [{
                required: true,
                message: messages('common.please.input')/*请输入*/
              }, {
                validator: (rule, value, callback) => {
                  console.log('value', value)
                  if (value && value.name) {
                    callback();
                  } else {
                    callback(messages('common.please.input')/*请输入*/);
                  }
                }
              }],
              initialValue: {name: record.listName, i18n: record.listNameI18n}
            })(
              <InputLanguage
                textArea
                maxLength={[
                  { length: 50, language: 'zh_cn' },
                  { length: 150, language: 'en' }
                ]}
                placeholder={messages('common.please.input')/*请输入*/}
                extraProps={{ rows: 4 }}
                disabled={disabled}
              />
            )}
          </FormItem>
          <FormItem label={messages('expense-14.key530')/*描述*/}>
            {getFieldDecorator('listDescI18n', {
              initialValue: {name: record.listDesc, i18n: record.listDescI18n}
            })(
              <InputLanguage
                textArea
                maxLength={[
                  { length: 200, language: 'zh_cn' },
                  { length: 600, language: 'en' }
                ]}
                placeholder={messages('common.please.input')/*请输入*/}
                extraProps={{ rows: 4 }}
                disabled={disabled}
              />
            )}
          </FormItem>
        </Form>
      </div>
    );
  }
}

function mapStateToProps(state) {
  return {
    tenant: state.login.tenant
  }
}

export default BlackWhiteBasicInfo;
